package com.example.springboot.service.impl;

import com.example.springboot.mapper.CustomerMapper;
import com.example.springboot.model.Customer;
import com.example.springboot.model.CustomerExample;
import com.example.springboot.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired private CustomerMapper customerMapper;
    @Override
    public Page<List<Customer>> getListPage(Customer customer, Integer pageNum, Integer pageSize) {
        CustomerExample example=new CustomerExample();
        if(!StringUtils.isEmpty(customer.getCustomerName())){
            example.createCriteria().andCustomerNameLike("%"+customer.getCustomerName()+"%");
        }
        Page<List<Customer>> page=PageHelper.startPage(pageNum,pageSize);
        customerMapper.selectByExample(example);
        return page;
    }

    @Override
    public void saveOrUpdate(Customer customer) {
        if(customer.getId()==null){
            customer.setCreateTime(new Date());
            customerMapper.insert(customer);
        }else{
            customerMapper.updateByPrimaryKeySelective(customer);
        }
    }

    @Override
    public void del(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }
}
